package com.htt.app.bienbaogiaothong;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.google.analytics.tracking.android.EasyTracker;
import com.htt.app.bienbaogiaothong.adapter.ListAdapter;
import com.htt.app.bienbaogiaothong.dialog.MyProgressDialog;
import com.htt.app.bienbaogiaothong.menu.Menu;
import com.htt.app.bienbaogiaothong.objects.Details;
import com.htt.app.bienbaogiaothong.sqlite.DAODetails;
import com.htt.app.bienbaogiaothong.utils.Utils;

public class ListActivity extends Activity implements OnClickListener{
	private List<Details> data = null;
	private int CategoriesId;
	private String CategoriesTitle;
	
	private Context ctx;
	private MyProgressDialog progressDialog;
	
	ImageButton btnMenu;
	ImageButton btnSearch;
	TextView tvTitle;
	ListView list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		ctx = this;
		
		CategoriesId = getIntent().getIntExtra("id", 0);
		CategoriesTitle = getIntent().getStringExtra("title");
		initLayout();
		tvTitle.setText(CategoriesTitle);
	    
		loadData();
		
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}
	
	private void initLayout() {
		btnMenu = (ImageButton) findViewById(R.id.list_btnMenu);
		btnSearch = (ImageButton) findViewById(R.id.list_btnSearch);
		tvTitle = (TextView) findViewById(R.id.list_title);
		list = (ListView) findViewById(R.id.list);
		
		btnMenu.setOnClickListener(this);
		btnSearch.setOnClickListener(this);
	}
	
	private void loadData() {
		new AsyncTask<Void, Void, Void>() {
			
			protected void onPreExecute() {
				progressDialog = new MyProgressDialog(ctx);
				progressDialog.startProgress("", ctx.getString(R.string.progress_load));
			};
			
			@Override
			protected Void doInBackground(Void... params) {
				// TODO Auto-generated method stub
				try {
					DAODetails db = new DAODetails(ctx);
					db.createDatabase();
					db.open();
					data = db.getChannelByCategories(CategoriesId);
					db.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
			
			protected void onPostExecute(Void result) {
				progressDialog.stopProgress();
				if (Utils.haveNetworkConnection(ctx)) {
					try {
						ListAdapter adapter = new ListAdapter(ctx, data);
						list.setAdapter(adapter);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					Utils.showDialog(ctx,
							ctx.getResources()
									.getString(R.string.error_connect),
		ctx.getResources().getString(R.string.connect_info));
				}
			};
			
		}.execute();
		
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == btnMenu)
			Menu.rateApp(this);
		else if (v == btnSearch) {
			Menu.startSearch(this);
		}
	}

}
